﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 记事本
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 存储打开路径的历史记录
        /// </summary>
        List<String> filePathList =new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        /// <summary>
        /// 隐藏浏览记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 显示浏览记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }
        /// <summary>
        /// 隐藏历史浏览记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }
        /// <summary>
        /// 打开并赋值给文本框文件信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog();
            OFD.InitialDirectory = @"D:\C#project\记事本";
            OFD.Filter = "txt files (*.txt)|*.txt| All files (*.*)| *.*";
            OFD.Multiselect = true;
            OFD.ShowDialog();
            ///获取路径及文件名
            String filePath = OFD.FileName;
            if (filePath == "") return;
            //检测是否为同一路径不允许同一文件加入同一路径
            bool FileXX = true;
            foreach (String file in filePathList) {
                if (file.Equals(filePath)) {
                    FileXX = false;
                    MessageBox.Show("文件重复！");
                } 
            }
            if (FileXX)
            { 
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read)) {
                byte[] by = new byte[1024*1024*5];
                int textlength = fs.Read(by, 0, by.Length);
                String txtText = Encoding.UTF8.GetString(by,0,textlength);
                textBox1.Text = txtText;
            }
            filePathList.Add(filePath);
            String sName = Path.GetFileName(filePath);
            listBox1.Items.Add(sName);
            }

        }
        /// <summary>
        /// 保存修改后的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog SFD = new SaveFileDialog();
            SFD.InitialDirectory = @"D:\C#project\记事本";
            SFD.Filter = "txt files (*.txt)|*.txt| All files (*.*)| *.*";
            SFD.ShowDialog();
            ///获取textbox的信息
            String textboxTxt = textBox1.Text;
            ///获取路径及文件名
            String filePath = SFD.FileName;
            if (filePath == "") return;
            using (FileStream fs=new FileStream(filePath,FileMode.Create,FileAccess.Write)) {
                byte[] by = Encoding.UTF8.GetBytes(textboxTxt);
                fs.Write(by,0, by.Length);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            //获取当前选择的文本路径
            String Nowfilepath = filePathList[index];
            using (FileStream file = new FileStream(Nowfilepath,FileMode.Open,FileAccess.Read)) {
                byte[] by = new byte[1024*1024*5];
                int r = file.Read(by, 0,by.Length);
                String text = Encoding.UTF8.GetString(by,0,r);
                textBox1.Text = text;
            }
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (自动换行ToolStripMenuItem.Text == "自动换行")
            {
                textBox1.WordWrap = true;
                自动换行ToolStripMenuItem.Text = "取消自动换行";
            }
            else {
                textBox1.WordWrap = false;
                自动换行ToolStripMenuItem.Text = "自动换行";
            }
            
        }

        private void 字体样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.ShowDialog();
            textBox1.Font = fd.Font;
        }

        private void 颜色样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            textBox1.ForeColor = cd.Color;
        }
    }
}
